package com.bdsys.daoimpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bdsys.common.dao.BasicDaoAbstract;
import com.bdsys.dao.MessageDAO;
import com.bdsys.domain.MessageDomain;

public class MessageDAOImpl extends BasicDaoAbstract implements MessageDAO{

	public MessageDAOImpl(){
		super("message");
	}
	
	@Override
	public void addMessage(MessageDomain inDomain) {
		getJdbcTemplate().update(getSql(DEVICE_INSERT),
				new Object[] { inDomain.getCode(), inDomain.getName(),
				inDomain.getTimeType(), inDomain.getAvailable(), inDomain.getDescription() });
	}

	@Override
	public List<MessageDomain> selectByConditions(MessageDomain messageDo) {
		StringBuilder sb = new StringBuilder(getSql(DEVICE_SEARCH_ALL));
		List<String> list = new ArrayList<String>();
		boolean isWhere = false;
		if((messageDo.getCode() != null && !messageDo.getCode().isEmpty())){
			sb.append(" where code = ?");
			isWhere = true;
			list.add(messageDo.getCode());
		}
		
		if ((messageDo.getName() != null && !messageDo.getName().isEmpty())){
			if(isWhere){
				sb.append(" and name = ?");
			}else {
				sb.append(" where name = ?");
				
				isWhere = true;
			}
			
			list.add(messageDo.getName());
		}
		
		if ((messageDo.getTimeType() != null && !messageDo.getTimeType().isEmpty())){
			if(isWhere){
				sb.append(" and time_type = ?");
			}else {
				sb.append(" where time_type = ?");
				
				isWhere = true;
			}
			
			list.add(messageDo.getTimeType());
		}
		
		if ((messageDo.getAvailable() != null && !messageDo.getAvailable().isEmpty())){
			if(isWhere){
				sb.append(" and available = ?");
			}else {
				sb.append(" where available = ?");
				
				isWhere = true;
			}
			
			list.add(messageDo.getAvailable());
		}
		
		if ((messageDo.getDescription() != null && !messageDo.getDescription().isEmpty())){
			if(isWhere){
				sb.append(" and description = ?");
			}else {
				sb.append(" where description = ?");
			}
			
			list.add(messageDo.getDescription());
		}
		
		List<Map<String, Object>> resultList =  getJdbcTemplate().queryForList(sb.toString(), list.toArray());
		List<MessageDomain> outDomainList = new ArrayList<MessageDomain>();
		for(Map<String, Object> outMap : resultList){
			MessageDomain outDomain = this.fromMapToDomain(outMap);
			outDomainList.add(outDomain);
		}
		
		return outDomainList;
	}
	
	@Override
	public void updateMessage(MessageDomain inDomain) {
		getJdbcTemplate().update(getSql(DEVICE_UPDATE),
				new Object[] { inDomain.getName(),
				inDomain.getTimeType(), inDomain.getAvailable(), inDomain.getDescription(), inDomain.getCode() });
	}
	
	public MessageDomain fromMapToDomain(Map<String, Object> map){
		MessageDomain domain = new MessageDomain();
		domain.setCode(String.valueOf(map.get("code")));
		domain.setName(String.valueOf(map.get("name")));
		domain.setTimeType(String.valueOf(map.get("time_type")));
		domain.setAvailable(String.valueOf(map.get("available")));
		domain.setDescription(String.valueOf(map.get("description")));
		return domain;
	}

	@Override
	public void deleteMessage(MessageDomain inDomain) {
		getJdbcTemplate().update(getSql(DEVICE_DELETE),
				new Object[] { inDomain.getCode()});
		
	}

	@Override
	public List<String> selectAllMessageCode() {
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(getSql(SEARCH_ALL_DEVICECODE), new Object[]{});
		List<String> messagesCode = new ArrayList<String>();
		for(Map<String, Object> map : list){
			messagesCode.add(String.valueOf(map.get("code")));
		}
		return messagesCode;
	}


}
